package com.igeek.health.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.igeek.health.entity.Ordersetting;
import com.igeek.health.mapper.OrdersettingMapper;
import com.igeek.health.service.IOrdersettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fengqq
 * @since 2024-08-26
 */
@Service
public class OrdersettingServiceImpl extends ServiceImpl<OrdersettingMapper, Ordersetting> implements IOrdersettingService {

    //更新预约设置表的数据
    @Transactional
    @Override
    public boolean updateAll(List<Ordersetting> ordersettingList) {
        if(CollUtil.isEmpty(ordersettingList)){
            return false;
        }
        ordersettingList.forEach(ordersetting->edit(ordersetting));
        return true;
    }

    //根据年月查询预约设置信息
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Integer>> findDataByMonth(Integer year, Integer month) {
        //根据当前年月 获取 起始日期
        LocalDate begin = LocalDate.of(year, month, 1);
        Date beginDate = DateUtil.date(begin);
        //根据当前年月 获取 结束日期
        LocalDate end = LocalDate.of(year, month, begin.lengthOfMonth());
        Date endDate = DateUtil.date(end);

        //Date endDate = DateUtil.endOfMonth(beginDate);

        //执行查询
        LambdaQueryWrapper<Ordersetting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(Ordersetting::getOrderdate,beginDate,endDate);
        List<Ordersetting> ordersettingList = baseMapper.selectList(queryWrapper);
        //List<Ordersetting> --> List<Map<String, Integer>>
        List<Map<String, Integer>> mapList = ordersettingList.stream().map(ordersetting -> {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("date",DateUtil.dayOfMonth(ordersetting.getOrderdate()));
            map.put("number",ordersetting.getNumber());
            map.put("reservations",ordersetting.getReservations());
            return map;
        }).collect(Collectors.toList());
        return mapList;
    }

    //根据预约日期修改可预约数量
    @Transactional
    @Override
    public boolean updateOne(Ordersetting ordersetting) {
        this.edit(ordersetting);
        return true;
    }

    private void edit(Ordersetting ordersetting){
        //1.查询预约日期 是否存在数据
        LambdaQueryWrapper<Ordersetting> queryWrapper = new LambdaQueryWrapper<Ordersetting>()
                .eq(Ordersetting::getOrderdate, ordersetting.getOrderdate());
        Long count = baseMapper.selectCount(queryWrapper);

        if(count>0){
            //2.1根据预约日期有数据，则执行更新 更新的可预约数量
            //2.2根据预约日期 获取已预约数量  当可预约数量 超过 已预约数量 则可以更新成功
            //已预约数量
            Integer reservations = baseMapper.selectOne(queryWrapper).getReservations();
            //需要更新的可预约数量 超过 已预约数量
            if(ordersetting.getNumber()>=reservations){
                baseMapper.update(ordersetting,queryWrapper);
            }
        }else{
            //3.1 根据预约日期没有数据 则执行插入
            baseMapper.insert(ordersetting);
        }


    }
}
